import * as services from '../services/role';
const ENTITY = 'role';
export default {
    namespace:ENTITY,
    state:{
        list: [],//数据源
        pageNum:1,
        total:0, //存放着实际的总条数
        editVisible:false,//编辑窗口是否可见 默认为false
        isCreate:true,//窗口是二合的，新增和编辑共且一个模态窗口，所以需要用isCreate字段区分
        record:{},//代表当前的记录
        selectedRowKeys:[],//选中的行的ID数组
        selectedRows:[],//选中的对象的数组
        where:{},
        resourceModalVisible:false,//设置权限的模态窗口是否可见
        checkedKeys:[],//选中的ID数组
        resources:[],//所有的权限，注意它是一个树型结构
        userModalVisible:false,
        targetKeys:[],//已经穿过去的用户ID数组
        users:[]//所有的用户信息
    },
    reducers:{
        save(state,{payload}){
            return {...state,...payload}
        }
    },
    effects:{
        *list({payload:{pageNum,where}},{call,put,select}){
            if(pageNum){
                pageNum = parseInt(pageNum);
            }else{
                pageNum = yield select(state=>state[ENTITY].pageNum);
            }
            if(!where){
                where = yield select(state=>state[ENTITY].where);
            }
            let {list,total} = yield call(services.list,pageNum,where);
            yield put({type:'save',payload:{list,total,pageNum,where}});
        },
        *create({payload},{call,put}){
            yield call(services.create,payload);
            yield put({type:'list',payload:{}});
            yield put({type:'save',payload:{editVisible:false}});
        },
        *update({payload},{call,put}){
            yield call(services.update,payload);
            yield put({type:'list',payload:{}});
            yield put({type:'save',payload:{editVisible:false}});
        },
        *del({payload},{call,put}){
            yield call(services.del,payload);
            yield put({type:'list',payload:{pageNum:1}});
        },
        *delAll({},{call,put,select}){
            let ids = yield select(state=>state[ENTITY].selectedRowKeys);
            yield call(services.delAll,ids);
            yield put({type:'list',payload:{pageNum:1}});
        },
        *search({where},{call,put,select}){
            yield put({type:'list',payload:{pageNum:1,where}});
        },
        *getResources({},{call,put,select}){
            let resources = yield call(services.getResources);
            yield put({type:'save',payload:{resources}});
        },
        *setResources({},{call,put,select}){
            let roleId = yield select(state=>state.role.record.id);
            let resourceIds = yield select(state=>state.role.checkedKeys);
            yield call(services.setResources,roleId,resourceIds);
            yield put({type:'list',payload:{}});
            yield put({type:'save',payload:{resourceModalVisible:false,
                selectedRows:[],selectedRowKeys:[],record:{}}});
        },
        *getUsers({},{call,put,select}){
            let users = yield call(services.getUsers);
            yield put({type:'save',payload:{users}});
        },
        *setUsers({},{call,put,select}){
            let roleId = yield select(state=>state.role.record.id);
            let userIds = yield select(state=>state.role.targetKeys);
            yield call(services.setUsers,roleId,userIds);
            yield put({type:'list',payload:{}});
            yield put({type:'save',payload:{userModalVisible:false,
                selectedRows:[],selectedRowKeys:[],record:{}}});
        }
    },
    //订阅外部数据源
    subscriptions:{
        setup({dispatch,history}){
            // /admin/user?pageNum=1&pageSize=3    pathname=/admin/user  query={pageNum:1}
            history.listen(function({pathname,query}){//监听路由的变化 
                if(pathname==`/admin/${ENTITY}`){
                    dispatch({type:'list',payload:query});
                    dispatch({type:'getResources'});
                    dispatch({type:'getUsers'});
                }
            });
        }
    }
}